package org.epzilla.dispatcher.dataManager;

import org.epzilla.dispatcher.clusterHandler.EventSender;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: chathura
 * Date: May 2, 2010
 * Time: 4:24:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class EventManager {
    private static ArrayList<String> ipArr = new ArrayList<String>();
    private static ArrayList<String> idArr = new ArrayList<String>();
    private static boolean isLoaded = false;

    public static void sendEvents(byte[] event, String clientID) {
        if (!isLoaded) {
            loadClusterDetails();
        }

        try {
            for (int i = 0; i < ipArr.size(); i++) {
                EventSender.sendEvent(event, ipArr.get(i), idArr.get(i), clientID);
            }
            EventsCounter.setOutEventCount();

        } catch (MalformedURLException e) {
            System.err.println(e);
        } catch (NotBoundException e) {
            System.err.println(e);
        } catch (RemoteException e) {
            System.err.println(e);
        }

    }

    private static void loadClusterDetails() {

        ipArr = ClusterLeaderIpListManager.getClusterIpList();
        idArr = ClusterLeaderIpListManager.getClusterIdList();
        isLoaded = true;
    }
}
